//
//  IFreeConnect.m
//  IFreeAppDemo
//
//  Created by qianfeng on 15/11/3.
//  Copyright © 2015年 qianfeng. All rights reserved.
//

#import "IFreeConnect.h"
#import "AFNetworking.h"
#import "LimitFreeModel.h"
#import "FMDBManager.h"

@implementation IFreeConnect
{
    AFHTTPRequestOperationManager * _manager;
}

+ (id)shareInstance
{
    static dispatch_once_t onceToken;
    static IFreeConnect * connect = nil;
    dispatch_once(&onceToken, ^{
        connect = [[IFreeConnect alloc] init];
    });
    return connect;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _manager = [AFHTTPRequestOperationManager manager];
        // 设置返回内容为二进制
        _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    }
    return self;
}

- (void)limitFreeURL:(NSString *)url result:(void (^)(id, NSError *))cb
{
    // 异步函数
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 一定要服务器给出响应后才有可能执行
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray * arr = [NSMutableArray array];
        for (NSDictionary * temp in dic[@"applications"]) {
            LimitFreeModel * m = [[LimitFreeModel alloc] init];
            // KVC保存属性键值对
            [m setValuesForKeysWithDictionary:temp];
            [arr addObject:m];
            
            // 插入数据库
            FMDBManager * manager = [FMDBManager shareInstance];
            [manager insertIntoDB:m];
        }
        // cb指向了在LimitFreeCtl中实现的函数
        cb(arr,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        cb(nil,error);
    }];
    // 直接在本地执行 非常快
    // NSLog(@".....");
}

- (void)limitSearchURL:(NSString *)url result:(void (^)(id, NSError *))cb
{
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray * arr = [NSMutableArray array];
        for (NSDictionary * temp in dic[@"applications"]) {
            LimitFreeModel * m = [[LimitFreeModel alloc] init];
            [m setValuesForKeysWithDictionary:temp];
            [arr addObject:m];
        }
        cb(arr,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)detailURL:(NSString *)url result:(void (^)(id, NSError *))cb
{
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        cb(dic,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)aroundURL:(NSString *)url result:(void (^)(id, NSError *))cb
{
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray * arr = [NSMutableArray array];
        for (NSDictionary * temp in dic[@"applications"]) {
            LimitFreeModel * m = [[LimitFreeModel alloc] init];
            [m setValuesForKeysWithDictionary:temp];
            [arr addObject:m];
        }
        cb(arr,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

@end
